/*
 * This file is part of the Sx Framework Library.
 * 
 * Copyright (C) 2013 University of Colorado Denver
 * <min.choi@ucdenver.edu> <shane.transue@ucdenver.edu>
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy 
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 
 * copies of the Software, and to permit persons to whom the Software is 
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in 
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
 * DEALINGS IN THE SOFTWARE.
 */
#include <sxDiscretePhysicalRepresentation.h>

void Sx::Physics::DiscretePhysicalRepresentation::addNode(const Eigen::Vector3d& node) {
	this->vertices.add(node);
	this->state.velocities.add(Eigen::Vector3d::Zero());
	this->state.accelerations.add(Eigen::Vector3d::Zero());
	this->state.internalForces.add(Eigen::Vector3d::Zero());
	this->state.externalForces.add(Eigen::Vector3d::Zero());
}

void Sx::Physics::DiscretePhysicalRepresentation::addVertex(const Eigen::Vector3d& vertex) {
	this->vertices.add(vertex);
	this->state.velocities.add(Eigen::Vector3d::Zero());
	this->state.accelerations.add(Eigen::Vector3d::Zero());
	this->state.internalForces.add(Eigen::Vector3d::Zero());
	this->state.externalForces.add(Eigen::Vector3d::Zero());
}

void Sx::Physics::DiscretePhysicalRepresentation::addVertexNormal(const Eigen::Vector3d& normal) {
	this->vertexNormals.add(normal);
}

void Sx::Physics::DiscretePhysicalRepresentation::addFaceNormal(const Eigen::Vector3d& normal) {
	this->faceNormals.add(normal);
}

void Sx::Physics::DiscretePhysicalRepresentation::setIntegrator(DiscreteIntegrator* integrator) {
	this->integrator = integrator;
}

Sx::Physics::DiscreteIntegrator* const Sx::Physics::DiscretePhysicalRepresentation::getIntegrator() const {
	return this->integrator;
}

Eigen::Vector3d Sx::Physics::DiscretePhysicalRepresentation::getVertex(unsigned int index) const {
	if ( index >= this->vertices.size() ) return Eigen::Vector3d::Zero();
	return this->vertices[index];
}

Eigen::Vector3d Sx::Physics::DiscretePhysicalRepresentation::getNodePrevPosition(unsigned int index) const {
	if ( index >= this->previousVertices.size() ) return Eigen::Vector3d::Zero();
	return this->previousVertices[index];
}

Eigen::Vector3d Sx::Physics::DiscretePhysicalRepresentation::getNodePrevVelocity(unsigned int index) const {
	if ( index >= this->state.previousVelocities.size() ) return Eigen::Vector3d::Zero();
	return this->state.previousVelocities[index];
}

Eigen::Vector3d Sx::Physics::DiscretePhysicalRepresentation::getVertexNormal(unsigned int index) const {
	if ( index >= this->vertices.size() ) return Eigen::Vector3d::Zero();
	return this->vertexNormals[index];
}

Eigen::Vector3d Sx::Physics::DiscretePhysicalRepresentation::getFaceNormal(unsigned int index) const {
	if ( index >= this->vertices.size() ) return Eigen::Vector3d::Zero();
	return this->faceNormals[index];
}

Sx::Util::ArrayList<Eigen::Vector3d>& Sx::Physics::DiscretePhysicalRepresentation::getVertices() {
	return this->vertices;
}

Sx::Util::ArrayList<Eigen::Vector3d>& Sx::Physics::DiscretePhysicalRepresentation::getPreviousVertices() {
	return this->previousVertices;
}

Sx::Util::ArrayList<Eigen::Vector3d>& Sx::Physics::DiscretePhysicalRepresentation::getVertexNormals() {
	return this->vertexNormals;
}

Sx::Util::ArrayList<Eigen::Vector3d>& Sx::Physics::DiscretePhysicalRepresentation::getFaceNormals() {
	return this->faceNormals;
}

bool Sx::Physics::DiscretePhysicalRepresentation::updatePreviousVertices() {
	this->previousVertices.clear();
	this->previousVertices.resize(this->vertices.size());
	for ( unsigned int i = 0; i < this->vertices.size(); i++ )
		this->previousVertices[i] = this->vertices[i];

	this->state.previousVelocities.clear();
	this->state.previousVelocities.resize(this->state.velocities.size());
	for ( unsigned int i = 0; i < this->state.velocities.size(); i++ )
		this->state.previousVelocities[i] = this->state.velocities[i];

	return true;
}
